-
Notifications
You must be signed in to change notification settings - Fork 13.3k
sync::mpsc: prevent double free on Drop
#139553
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
cc @ibraheemdev |
Is it possible add a test reproducing the failure? Ideally something |
I can try to find a miri seed that leads to the problematic thread scheduling but I'm worried that if the code of the channel implementation changes or the version of miri changes the tested schedule will change as well and the test won't be testing what we think it does anymore. |
I accidentally clicked the close button, sorry! |
I think it's fine to just include an isolated test that makes conditions for this problem favorable, extra good if's reasonably fast to provide a seed+sha for which Miri shows the problem locally. Cc @RalfJung |
@tgross35 I have been running Miri with the use std::sync::mpsc::channel;
use std::thread;
fn main() {
let (tx, rx) = channel::<i32>();
let tx2 = tx.clone();
let t1 = thread::spawn(move || {
let _ = tx.send(1);
});
tx2.send(1).unwrap();
drop(rx);
let _ = t1.join();
} It is fairly easy to reproduce manually by adding a sleep at the right place and running the test of this commit adapter to std's channels crossbeam-rs/crossbeam@00dcea6. I will leave Miri running overnight and report back with any interesting seeds. |
Does the bug reproduce in Miri with the Maybe we could have a |
After leaving Miri running overnight it didn't find a suitable seed that produces the interleaving that causes the double free. I'm not sure why this interleaving is hard to reach. I pushed a commit here petrosagg@79372e9 that adds a sleep to make the specific thread interleaving favorable and the problem reproduces with both normal tests and Miri. You can reproduce it with either The normal test will print
|
If you make this |
I will try reproducing with just |
I'm not able to reproduce it even with a |
8939b53
to
19219f5
Compare
The Miri subtree was changed cc @rust-lang/miri |
I managed to repro it with Miri and simple thread yields! I think the default |
Is it your estimation that the default failure rate is too high, or is it too low? |
Too high. My test is basically baking a thread schedule (by calling That said, the overnight test looking for seed values had only one |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have tests/pass/issues
for tests testing specific issues. Also please put a comment in the file explaining the context of the test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I totally missed that directory. I moved the test there and added detailed comments explaining the particular interleaving and the way the double free is caused.
@@ -213,6 +213,8 @@ impl<T> Channel<T> { | |||
.compare_exchange(block, new, Ordering::Release, Ordering::Relaxed) | |||
.is_ok() | |||
{ | |||
#[cfg(miri)] | |||
crate::thread::yield_now(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem with this is that this will then always be present when Miri executes this code, even outside the test. Ideally we'd only enable this on our CI. This will require some new flag though...
Also there should be a comment explaining the point of this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a comment explaining why this point is interesting and a pointer to the test that uses it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
I guess now the main question is whether it's worth leaving in this yield
for all Miri users or whether it should be somehow only applied for the test suite. It probably doesn't harm to always have it...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would also lean towards leaving there. It does yield at a very interesting point of the channel in general so having it there might catch future regressions sooner
19219f5
to
63fafe3
Compare
Signed-off-by: Petros Angelatos <[email protected]>
63fafe3
to
72c9a47
Compare
This PR is fixing a regression introduced by rust-lang#121646 that can lead to a double free when dropping the channel. The details of the bug can be found in the corresponding crossbeam PR crossbeam-rs/crossbeam#1187 Signed-off-by: Petros Angelatos <[email protected]>
72c9a47
to
b9e2ac5
Compare
…alfJung,tgross35 sync::mpsc: prevent double free on `Drop` This PR is fixing a regression introduced by rust-lang#121646 that can lead to a double free when dropping the channel. The details of the bug can be found in the corresponding crossbeam PR crossbeam-rs/crossbeam#1187
Sorry I'm very busy as of late, but from a cursory look this seems good. |
…iaskrgr Rollup of 9 pull requests Successful merges: - rust-lang#138528 (deref patterns: implement implicit deref patterns) - rust-lang#138934 (support config extensions) - rust-lang#139114 (Implement `pin!()` using `super let`) - rust-lang#139393 (rustdoc-json: Output target feature information) - rust-lang#139553 (sync::mpsc: prevent double free on `Drop`) - rust-lang#139615 (Remove `name_or_empty`) - rust-lang#139853 (Disable combining LLD with external llvm-config) - rust-lang#139913 (rustdoc/clean: Fix lowering of fn params (fixes correctness & HIR vs. middle parity regressions)) - rust-lang#139942 (Ignore aix for tests/ui/erros/pic-linker.rs) r? `@ghost` `@rustbot` modify labels: rollup
…alfJung,tgross35 sync::mpsc: prevent double free on `Drop` This PR is fixing a regression introduced by rust-lang#121646 that can lead to a double free when dropping the channel. The details of the bug can be found in the corresponding crossbeam PR crossbeam-rs/crossbeam#1187
…iaskrgr Rollup of 8 pull requests Successful merges: - rust-lang#138528 (deref patterns: implement implicit deref patterns) - rust-lang#138934 (support config extensions) - rust-lang#139393 (rustdoc-json: Output target feature information) - rust-lang#139553 (sync::mpsc: prevent double free on `Drop`) - rust-lang#139615 (Remove `name_or_empty`) - rust-lang#139853 (Disable combining LLD with external llvm-config) - rust-lang#139913 (rustdoc/clean: Fix lowering of fn params (fixes correctness & HIR vs. middle parity regressions)) - rust-lang#139942 (Ignore aix for tests/ui/erros/pic-linker.rs) r? `@ghost` `@rustbot` modify labels: rollup
…iaskrgr Rollup of 8 pull requests Successful merges: - rust-lang#138528 (deref patterns: implement implicit deref patterns) - rust-lang#138934 (support config extensions) - rust-lang#139393 (rustdoc-json: Output target feature information) - rust-lang#139553 (sync::mpsc: prevent double free on `Drop`) - rust-lang#139615 (Remove `name_or_empty`) - rust-lang#139853 (Disable combining LLD with external llvm-config) - rust-lang#139913 (rustdoc/clean: Fix lowering of fn params (fixes correctness & HIR vs. middle parity regressions)) - rust-lang#139942 (Ignore aix for tests/ui/erros/pic-linker.rs) r? `@ghost` `@rustbot` modify labels: rollup
…iaskrgr Rollup of 7 pull requests Successful merges: - rust-lang#138528 (deref patterns: implement implicit deref patterns) - rust-lang#139393 (rustdoc-json: Output target feature information) - rust-lang#139553 (sync::mpsc: prevent double free on `Drop`) - rust-lang#139615 (Remove `name_or_empty`) - rust-lang#139853 (Disable combining LLD with external llvm-config) - rust-lang#139913 (rustdoc/clean: Fix lowering of fn params (fixes correctness & HIR vs. middle parity regressions)) - rust-lang#139942 (Ignore aix for tests/ui/erros/pic-linker.rs) r? `@ghost` `@rustbot` modify labels: rollup
…iaskrgr Rollup of 7 pull requests Successful merges: - rust-lang#138528 (deref patterns: implement implicit deref patterns) - rust-lang#139393 (rustdoc-json: Output target feature information) - rust-lang#139553 (sync::mpsc: prevent double free on `Drop`) - rust-lang#139615 (Remove `name_or_empty`) - rust-lang#139853 (Disable combining LLD with external llvm-config) - rust-lang#139913 (rustdoc/clean: Fix lowering of fn params (fixes correctness & HIR vs. middle parity regressions)) - rust-lang#139942 (Ignore aix for tests/ui/erros/pic-linker.rs) r? `@ghost` `@rustbot` modify labels: rollup
…iaskrgr Rollup of 7 pull requests Successful merges: - rust-lang#138528 (deref patterns: implement implicit deref patterns) - rust-lang#139393 (rustdoc-json: Output target feature information) - rust-lang#139553 (sync::mpsc: prevent double free on `Drop`) - rust-lang#139615 (Remove `name_or_empty`) - rust-lang#139853 (Disable combining LLD with external llvm-config) - rust-lang#139913 (rustdoc/clean: Fix lowering of fn params (fixes correctness & HIR vs. middle parity regressions)) - rust-lang#139942 (Ignore aix for tests/ui/erros/pic-linker.rs) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of rust-lang#139553 - petrosagg:channel-double-free, r=RalfJung,tgross35 sync::mpsc: prevent double free on `Drop` This PR is fixing a regression introduced by rust-lang#121646 that can lead to a double free when dropping the channel. The details of the bug can be found in the corresponding crossbeam PR crossbeam-rs/crossbeam#1187
[beta] backports - Fix 2024 edition doctest panic output rust-lang#139328 - make `Arguments::as_statically_known_str` doc(hidden) rust-lang#139389 - Revert "Deduplicate template parameter creation" rust-lang#139878 - sync::mpsc: prevent double free on `Drop` rust-lang#139553 r? cuviper
…alfJung,tgross35 sync::mpsc: prevent double free on `Drop` This PR is fixing a regression introduced by rust-lang#121646 that can lead to a double free when dropping the channel. The details of the bug can be found in the corresponding crossbeam PR crossbeam-rs/crossbeam#1187
…iaskrgr Rollup of 7 pull requests Successful merges: - rust-lang#138528 (deref patterns: implement implicit deref patterns) - rust-lang#139393 (rustdoc-json: Output target feature information) - rust-lang#139553 (sync::mpsc: prevent double free on `Drop`) - rust-lang#139615 (Remove `name_or_empty`) - rust-lang#139853 (Disable combining LLD with external llvm-config) - rust-lang#139913 (rustdoc/clean: Fix lowering of fn params (fixes correctness & HIR vs. middle parity regressions)) - rust-lang#139942 (Ignore aix for tests/ui/erros/pic-linker.rs) r? `@ghost` `@rustbot` modify labels: rollup
This PR is fixing a regression introduced by #121646 that can lead to a double free when dropping the channel.
The details of the bug can be found in the corresponding crossbeam PR crossbeam-rs/crossbeam#1187